package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.BrandService;
import com.yoda.mall.model.entity.pms.Brand;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangjia
 * @date 2022/12/16
 * @Description
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 添加品牌
     *
     * @param
     * @return
     */
    @PostMapping("/create")
    public R addBrand(Brand brand) {

        //调用service处理前端传递过来的数据
        brandService.addBrand(brand);
        //返回
        return R.success("添加成功");
    }

    //删除品牌
    @GetMapping("/delete/{id}")
    public R deleteBrand(@PathVariable(value = "id") Integer id) {
        brandService.delete(id);
        return R.success("删除成功");
    }

    //批量删除
    @PostMapping("/delete/batch")
    public R deleteIds(@RequestParam("ids") List<Integer> ids) {
        brandService.deleteIds(ids);
        return R.success("删除成功");
    }

   //根据品牌名称查询分类列表
    @GetMapping("/list")
    public R list(String keyword, @RequestParam(defaultValue = "1") Integer pageNum,
               @RequestParam(defaultValue = "5") Integer pageSize) {
        PageResult pageResult = brandService.findBrand(keyword,pageNum,pageSize);
        return R.success(pageResult);
    }

    /**
     * 获取全部品牌列表
     * @return
     */
    @GetMapping("/listAll")
    public R<List<Brand>> listAll() {
        List<Brand> list = brandService.listAll();
        return R.success(list);
    }

    //批量更新制造商状态
    @PostMapping("/update/factoryStatus")
    public R updateStatus(@RequestParam Integer factoryStatus,@RequestParam List<Long> ids) {
        brandService.updateStatus(factoryStatus,ids);
        return R.success("修改成功");
    }

    //批量更新制造商状态
    @PostMapping("/update/showStatus")
    public R updateShowstatus(@RequestParam Integer showStatus,@RequestParam List<Long> ids) {
        brandService.updateShowstatus(
                showStatus,ids);
        return R.success("修改成功");
    }

    //更新品牌
    @PostMapping("/update/{id}")
    public R updateBrand(@PathVariable(value = "id")Long id, Brand brand){
        brand.setId(id);
        brandService.updateBrand(brand);
        return R.success("更新成功");
    }

    //根据id查询品牌详情
    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Long id) {
       Brand brand = brandService.findById(id);
        return R.success(brand);
    }

}